<?php

class NewsController extends Controller {
    
    public function actionIndex() {
        $news_criteria = new CDbCriteria;
        $news_criteria->condition = "news_is_active = '1'";
        $news_criteria->order = "news_input_datetime DESC";

        $news_pagination = new CPagination(site_news::model()->count($news_criteria));
        $news_pagination->pageSize = Yii::app()->params['newsPerPage'];
        $news_pagination->applyLimit($news_criteria);

        $news_model = site_news::model()->findAll($news_criteria);

        $this->render('index', array('news_model' => $news_model, 'news_pagination' => $news_pagination));
    }

    public function actionDetail() {
        $news_model = site_news::model()->findByPk($_GET['id']);

        $news_tag_criteria = new CDbCriteria;
        $news_tag_criteria->condition = "news_tag_news_id = '{$_GET['id']}'";
        $news_tag_model = site_news_tag::model()->findAll($news_tag_criteria);
        if ($news_tag_model != null) {
            $news_tag_condition = '(';
            $i = 1;
            foreach ($news_tag_model as $row_news_tag) {
                $news_tag_condition .= "news_tag_value = '{$row_news_tag->news_tag_value}'";
                if($i < count($news_tag_model)) $news_tag_condition .= " OR ";
                $i++;
            }
            $news_tag_condition .= ") AND news_tag_news_id != '{$_GET['id']}'";
            $other_news_criteria = new CDbCriteria;
            $other_news_criteria->condition = $news_tag_condition;
            $other_news_criteria->group = "news_tag_news_id";
            $other_news_model = site_news_tag::model()->with('rel_news_tag_to_news')->findAll($other_news_criteria);
        }
        else $other_news_model = null;

        $this->render('detail', array(
            'news_model' => $news_model,
            'news_tag_model' => $news_tag_model,
            'other_news_model' => $other_news_model,
        ));
    }

    public function actionTag() {
        $news_tag_value = str_replace("-", " ", $_GET['value']);
        $news_tag_criteria = new CDbCriteria;
        $news_tag_criteria->condition = "news_tag_value = '{$news_tag_value}'";
        $news_tag_criteria->group = "news_tag_news_id";
        $news_tag_model = site_news_tag::model()->with('rel_news_tag_to_news')->findAll($news_tag_criteria);

        $news_tag_pagination = new CPagination(count($news_tag_model));
        $news_tag_pagination->pageSize = Yii::app()->params['newsPerPage'];
        $news_tag_pagination->applyLimit($news_tag_criteria);

        $this->render('tag', array(
            'news_tag_value' => $news_tag_value,
            'news_tag_model' => $news_tag_model,
            'news_tag_pagination' => $news_tag_pagination,
        ));
    }

}
